<?php include('includes/start.php'); ?>

<?php
echo "<h2>Book Search</h2>";
$con = mysql_connect($dbserver, $dbuser, $dbpass);
if (!$con)
{
	echo "<p>Connection to server failed</p>";
}
else
{
	mysql_select_db($db, $con);
	echo "<div id='book_results'>";
	
	if ($_GET['keywords']){
		
		
		$pagenum = 1;
		$display = $_book_search_page_rows;
	
		if(isset($_GET['p']))
			$pagenum = $_GET['p'];
		if(isset($_GET['d']))
			$display = $_GET['d'];
				
		
		$q = $book_keyword_search;
		$qcount = $book_keyword_search_count;
		$sq = $book_keyword_search_params; 
		$q = split_query_replace($q, $sq,$_GET['keywords']);
		$qcount = split_query_replace($qcount, $sq,$_GET['keywords']);
		$rows = mysql_query($qcount);
		$rows = mysql_fetch_array($rows);
		$rows = $rows['row_count'];
		
		$q = add_limit($q, ($pagenum-1)*$display, $display);
		$result = mysql_query($q);
		
		
		mysql_close($con);
		
		
		if (mysql_num_rows($result) == 0)
		{
			echo "No books found with keyword(s): " . $_GET['keywords'];
		}
		else
		{
			 echo $rows . " Results Found.";
			
			$last_page = ceil($rows/$display); 
			
			echo "<table id='DB_search_results'>";
			echo "	<tr>";
			echo "		<th id='book_search_img_table'></th>";
			echo "		<th>Title</th>";
			echo "		<th>Authors</th>";
			echo "		<th>Description</th>";
			echo "	</tr>";
			while($book = mysql_fetch_array( $result )) 
			{ 
			
				//echo "<tr><td id='DB_search_divider' colspan='4'></td></tr>";
			
				$file = $book['cover_image'];
				if (!file_exists($file))
				{
					$file = $book_not_found;
				}
				//$book_link = "<a href='book.php?id={$book['media_id']}'>";
				echo "<tr id='book_search_result' onclick=\"location.href = 'book.php?id={$book['media_id']}'\">";
				echo "<td id='book_search_img_Container'><img id='book_search_img' src=\"" . $file . "\" /></a></td>";
				echo "<td>" . $book['title'] . "</td>";
				echo "<td id='book_result_author' >" . $book['authors'] . "</td>";
				echo "<td>" . $book['description'] . "</td>";
				echo "</tr>";
				
				
			}
						
			echo "</table>";
			
			
		
			echo "</div>";
			
			if($last_page != 1)
			{
				echo "<div id='page_list'>";
				if($pagenum != 1)
				{
					echo "<a href='{$_SERVER['PHP_SELF']}?keywords={$_GET['keywords']}&p=1&d=".$display."'><< </a>";
					$previous = $pagenum -1;
					echo "<a href='{$_SERVER['PHP_SELF']}?keywords={$_GET['keywords']}&p=". $previous ."&d=".$display."'>< </a>";
				}
				else
				{
					echo "<< < ";
				}
				
				for ($i = 1; $i <= $last_page; $i++) {
					if($i != $pagenum)
						echo "<a href='{$_SERVER['PHP_SELF']}?keywords={$_GET['keywords']}&p=" . $i . "&d=".$display."'>". $i ." </a>";
					else
						echo $i ." ";
				}
				
				if($pagenum != $last_page)
				{
					$next = $pagenum + 1;
					echo "<a href='{$_SERVER['PHP_SELF']}?keywords={$_GET['keywords']}&p=" . $next . "&d=".$display."'>> </a>";
					echo "<a href='{$_SERVER['PHP_SELF']}?keywords={$_GET['keywords']}&p=" . $last_page . "&d=".$display."'>>></a>";
				}
				else
				{
					echo "> >>";
				}
				echo "</div>";
			}
		}
	}
	else
	{
			echo "Please enter keywords into search field.";
			echo "</div>";
	}
	
}
?>

<?php include('includes/stop.php'); ?>